//
//  DreamTwoViewController.m
//  Light
//
//  Created by 骁唐 on 15/9/17.
//  Copyright (c) 2015年 骁唐. All rights reserved.
//

#import "DreamTwoViewController.h"
#import "AppDelegate.h"
#import "SliderViewController.h"

@interface DreamTwoViewController ()<UIGestureRecognizerDelegate>
//内容展示view
@property (weak,nonatomic) IBOutlet UIView * viewBig;
//button背景图
@property (weak, nonatomic) IBOutlet UIImageView *btnBackGround;
//x关系
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xBtn;
//滑动区域
@property (weak, nonatomic) IBOutlet UIView *sliderView;
//详细内容描述
@property (weak, nonatomic) IBOutlet UILabel *labelDescripe;
//提示内容
@property (weak, nonatomic) IBOutlet UILabel *labelWarn;
//背景颜色
@property (weak, nonatomic) IBOutlet UIImageView *backGround;
//左滑手势
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
//右滑手势
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
//自定义提示view，需要切圆角
@property (nonatomic,weak) IBOutlet UIView * viewAlert;
//确认按钮
@property (nonatomic,weak) IBOutlet UIButton * buttonOK;
//取消按钮
@property (nonatomic,weak) IBOutlet UIButton * buttonFaile;
@end

@implementation DreamTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    UserConfig * config = [UserConfig sharingUserConfig];
    delegate.dreamImprove = [config isDreamImprove];//获取改善睡眠
    if (delegate.dreamImprove)
    {
        self.xBtn.constant = self.sliderView.frame.size.width/2;
        self.btnBackGround.image = [UIImage imageNamed:@"sleep - backgroundOn"];
        [self.view layoutIfNeeded];
    }
    else
    {
        self.xBtn.constant = 0;
        self.btnBackGround.image = [UIImage imageNamed:@"sleep - background"];
        [self.view layoutIfNeeded];
    }
    
    
    //梦境控制  文案详情 设置介绍文本字距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:MESSAGE_MENGJING_DESC];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:8];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[MESSAGE_MENGJING_DESC length])];
    [_labelDescripe setAttributedText:attributedString];
    [_labelDescripe sizeToFit];
    
    //梦境控制 文案提醒
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc]initWithString:MESSAGE_MENGJING_WARNING];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle1 setLineSpacing:5];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0,[MESSAGE_MENGJING_WARNING length])];
    [_labelWarn setAttributedText:attributedString1];
    [_labelWarn sizeToFit];
    
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.sliderView addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.sliderView addGestureRecognizer:self.rightSwipeGestureRecognizer];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark -
#pragma mark 开启梦境控制按钮 关闭梦境控制
- (IBAction)onBtnClick {
    [UIView animateWithDuration:0.3 animations:^{
        self.xBtn.constant = 0;
        self.btnBackGround.image = [UIImage imageNamed:@"sleep - background"];
        [self.view layoutIfNeeded];
    }];
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSData *data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_DREAMEND];
    if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
        [delegate.peripheral writeValue:data
                      forCharacteristic:delegate.writeCharacteristic
                                   type:CBCharacteristicWriteWithResponse];
    }
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(DreamKongZhi) userInfo:nil repeats:NO];
}

#pragma mark -
#pragma mark 关闭梦境控制按钮  打开梦境控制
- (IBAction)offBtnClick {
    [UIView animateWithDuration:0.3 animations:^{
        self.xBtn.constant = self.sliderView.frame.size.width/2;
        self.btnBackGround.image = [UIImage imageNamed:@"sleep - backgroundOn"];
        [self.view layoutIfNeeded];
    }];
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSData *data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_DREAMSTART];
    if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
        [delegate.peripheral writeValue:data
                      forCharacteristic:delegate.writeCharacteristic
                                   type:CBCharacteristicWriteWithResponse];
    }
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(DreamKongZhi) userInfo:nil repeats:NO];
}

#pragma mark -
#pragma mark 导航返回
- (IBAction)backClick {
//    [self.navigationController popViewControllerAnimated:YES];
    for (UIViewController *slider in self.navigationController.viewControllers) {
        if ([slider isKindOfClass:[SliderViewController class]]) {
            [self.navigationController popToViewController:slider animated:YES];
        }
    }
}
#pragma mark -
#pragma mark OK按钮提示
- (IBAction)buttonOkClick:(UIButton *)button
{
    //打开解除绑定弹窗
    [UIView animateWithDuration:0.5 animations:^{
        self.viewAlert.hidden = YES;
        self.viewBig.hidden =NO;//打开隐藏的内容view
    }];
}

#pragma mark -
#pragma mark 取消按钮，提示
- (IBAction)buttonFaileClick:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 梦境控制是否开启
- (void)DreamKongZhi
{
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    UserConfig * config = [UserConfig sharingUserConfig];
    delegate.dreamImprove = [config isDreamImprove];
    if (delegate.dreamImprove) {
        NSLog(@"已经开启");
        delegate.alertAutoOne = [[UIAlertView alloc] initWithTitle:nil message:REPORT_FOUR_ALTERTION_ON delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [delegate.alertAutoOne show];
        //0.8S隐藏
        [self performSelector:@selector(closeAlertViewOne) withObject:nil afterDelay:0];
    }else
    {//关闭
        [UIView animateWithDuration:0.3 animations:^{
            self.xBtn.constant = 0;
            self.btnBackGround.image = [UIImage imageNamed:@"sleep - background"];
            [self.view layoutIfNeeded];
        }];
        delegate.alertAutoOne = [[UIAlertView alloc] initWithTitle:nil message:REPORT_FOUR_ALTERTION_OFF delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [delegate.alertAutoOne show];
        //0.8S隐藏
        [self performSelector:@selector(closeAlertViewOne) withObject:nil afterDelay:0];
    }
}

#pragma mark -
#pragma mark 关闭弹窗One
- (void)closeAlertViewOne
{
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    [delegate.alertAutoOne dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    //设置提示view
    self.viewAlert.layer.cornerRadius = 4;
    self.viewAlert.layer.masksToBounds = YES;
    self.buttonOK.layer.cornerRadius = 20;
    self.buttonOK.layer.masksToBounds = YES;
    self.buttonFaile.layer.cornerRadius = 20;
    self.buttonFaile.layer.masksToBounds = YES;
    UserConfig * config = [UserConfig sharingUserConfig];
    delegate.dreamImprove = [config isDreamImprove];//获取改善睡眠
    if ([config isFirstDreemingControl])
    {//如果第一次使用该功能
        self.viewAlert.hidden = NO;//打开隐藏提示View
        self.viewBig.hidden =YES;//隐藏的内容view
        self.xBtn.constant = 0;
        self.btnBackGround.image = [UIImage imageNamed:@"sleep - background"];
        [self.view layoutIfNeeded];
    }
    else
    {//否则执行下面
        if (delegate.dreamImprove)
        {//如果梦境控制开启，设置开关的背景图片
            self.viewAlert.hidden = YES;//隐藏提示View
            self.viewBig.hidden =NO;//打开隐藏的内容view
            self.xBtn.constant = self.sliderView.frame.size.width/2;
            self.btnBackGround.image = [UIImage imageNamed:@"sleep - backgroundOn"];
            [self.view layoutIfNeeded];
        }
        else
        {
            self.viewAlert.hidden = YES;//隐藏提示View
            self.viewBig.hidden =NO;//打开隐藏的内容view
            self.xBtn.constant = 0;
            self.btnBackGround.image = [UIImage imageNamed:@"sleep - background"];
            [self.view layoutIfNeeded];
        }
    }
    [config setFirstDreemingControl:FIRSTDREEMINGCONTROL];//标记已经使用了提示
    
    long long int currentTimeInt = [TJJDefines getTimeIntSince1970Current];
    if (currentTimeInt>=16200&& currentTimeInt <=34200) {
        self.backGround.image = [UIImage imageNamed:@"backgroundOne"];
    }else if (currentTimeInt>34200 && currentTimeInt<=55800)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundTwo"];
    }else if (currentTimeInt>55800 && currentTimeInt<=70200)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundThree"];
    }else if(currentTimeInt >70200 || currentTimeInt<16200)
    {
        self.backGround.image = [UIImage imageNamed:@"background_night"];
    }
}

#pragma mark -
#pragma mark 滑动控制 华东控制
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionRight)
    {//右滑，打开梦境控制
        [UIView animateWithDuration:0.3 animations:^{
            self.xBtn.constant = self.sliderView.frame.size.width/2;
            self.btnBackGround.image = [UIImage imageNamed:@"sleep - backgroundOn"];
            [self.view layoutIfNeeded];
        }];
        
        AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
        NSData * data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_DREAMSTART];
        if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
            [delegate.peripheral writeValue:data
                          forCharacteristic:delegate.writeCharacteristic
             
                                       type:CBCharacteristicWriteWithResponse];
            
        }
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(DreamKongZhi) userInfo:nil repeats:NO];
    }
    else if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {//左滑，关闭梦境控制
        [UIView animateWithDuration:0.3 animations:^{
            self.xBtn.constant = 0;
            self.btnBackGround.image = [UIImage imageNamed:@"sleep - background"];
            [self.view layoutIfNeeded];
        }];
        AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
        NSData *data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_DREAMEND];
        if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
            [delegate.peripheral writeValue:data
                          forCharacteristic:delegate.writeCharacteristic
                                       type:CBCharacteristicWriteWithResponse];
        }
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(DreamKongZhi) userInfo:nil repeats:NO];
    }
}

@end
